<?php
namespace Common\Model;
use Think\Model;

/**
* 新闻模型基类
* 
* 新闻的查询、新增、编辑、删除等业务封装
* 
* @author: xuhui <xuh86@126.com>
* @version: $Id$
* @package: asoc.Common.Model
*/

class NewsModel extends Model{
	
	
	public function getItemById($id,$lang=''){
		
		$lang	=	$lang != '' ? $lang : LANG_SET;
		$map['news_id']	=	$id;
		$map['lang']	=	getLangShort($lang);
		$info	=	$this->where($map)->find();
		
		return $info;
	}
	
	/**
	 * 根据查询条件返回分页数据
	 * 
	 * @param mixed $map: 查询过滤条件
	 * @param int $currentPage:当前页
	 * @param int $pageSize: 每页大小
	 * @param string $sortBy: 排序条件
	 * @return array 
	 */
	public function getItemListBy($map,$currentPage=1,$pageSize=10,$sortBy=''){

		$items	=	NULL;
		$count	=	0;			//总条数
		
		//主键
		$pk		=	$this->getPk();

		//排序参数
		$sortBy	=	empty($sortBy) ? $pk." DESC " : $sortBy;
		if($currentPage>0 && $pageSize>0){	//分页查询
			$items	=	$this->where($map)->order($sortBy)->page($currentPage.','.$pageSize)->select();
		}else{//根据条件查询全部
			if($pageSize > 0){
				$items	=	$this->where($map)->order($sortBy)->limit($pageSize)->select();
			}else{
				$items	=	$this->where($map)->order($sortBy)->select();
			}
		}
		
		$count	=	$this->where($map)->count();
		return array('items'=>$items,'count'=>$count);
	}
	
	
	
	
	/**
	* 保存编辑和新增
	* @param array $data:保存数组
	* @return array
	*/
	public function actionSave($data){
	
		$statas	=	0;
		$msg	=	'操作出错';
		
		//主键
		$time	=	time();
		$pk		=	$this->getPk();
		
		if(isset($data['content'])){
			$data['content']	=	stripslashes($data['content']);
		}
		if(isset($data[$pk]) && $data[$pk]>0){
			
			$data['update_time']	=	$time;	
			//保存基本信息
			$this->where($pk."=".$data[$pk])->save($data);
			//操作日志
			$loginfo	=	'编辑资讯信息，ID：'.$data[$pk];
			D('Logs')->addAdminLog($loginfo,session(C('BE_UID')));
			
			$statas		=	1;
			$msg		=	'';
				
		}else{
			//保存基本信息
			$data['add_time']	=	$time;
			$data['update_time']	=	$time;
			$newId		=	$this->add($data);
			$statas		=	2;
			$msg		=	'保存出错！';
			
			if($newId>0){
				$statas		=	1;
				$msg		=	'';
				
				$adminId	=	session(C('BE_UID'));
				//操作日志
				$loginfo	=	'新增资讯信息，ID：'.$newId;
				D('Logs')->addAdminLog($loginfo,$adminId);
			}
		}
	
		return array('status'=>$statas,'msg'=>$msg,'newid'=>$newId);
	}
	
	
	
	/**
	 * 按条件删除记录
	 * @param mixed $condition: int/array 删除新闻的id   
	 * @return int:成功删除记录数
	 */
	public function deleteBy($condition){

		$delNum		=	0;
		
		$logModel	=	D('Logs');
		//删除单个
		if(is_numeric($condition)){
			if($this->find($condition)){
				if($this->deleteOne($condition))	$delNum++;
			}
		}
		//删除多个
		if(is_array($condition)){
			$num	=	count($condition);
			for($i=0;$i<$num;$i++){
				$recordId	=	$condition[$i];
				if($recordId>0){
					if($this->deleteOne($recordId))	$delNum++;
				}
			}
		}
		
		return $delNum;
	}
	
	
	
	/**
	 * 删除单条记录
	 * @param int $id:删除的记录id
	 * @return boolean
	 */
	private function deleteOne($id){
		
		$status	=	false;
		$pk		=	$this->getPk();
		$info	=	$this->find($id);
		if($info){
			$status	=	true;
			//删除记录
			$this->delete($id);
			//保存操作记录
			$adminId	=	session(C('BE_UID'));
			D('Logs')->addAdminLog('删除资讯,id:'.$id,$adminId);
		}
		return $status;
	}
	
}

?>